<?php
namespace app\api\service;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Certification extends Controller {

    /**
     * 获取实名认证信息
     * @param $param
     * @return array
     */
    public static function get_certification_info($param) {
        try {
            $certification = model('certification')->get(['member_id' => $param['member_id']]);
            $member_role_info = model('member_role_info')->where(['member_id' => $param['member_id']])->find();
            $data = [
                'headimgurl' => get_img_url($member_role_info->headimgurl),
                'nickname' => $member_role_info->nickname,
                'status' => 0,
                'status_text' => '未认证',
                'proposer' => '',
                'card_no' => '',
                'id_card_in_hand' => '',
                'id_card_front' => '',
                'id_card_verso' => '',
                'id' => 0,
            ];
            if ($certification) {
                $data['proposer'] = $certification->proposer;
                $data['card_no'] = $certification->card_no;
                $data['id_card_in_hand'] = $certification->id_card_in_hand;
                $data['id_card_front'] = $certification->id_card_front;
                $data['id_card_verso'] = $certification->id_card_verso;
                $data['status'] = $certification->status;
                $data['status_text'] = get_certification_status($certification->status);
                $data['id'] = $certification->id;
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, '', $e->getMessage());
        }
    }


    /**
     * 保存实名认证信息
     * @param $param
     * @return array
     */
    public static function save_certification($param) {
        Db::startTrans();
        try {
            // 如果ID == 0 表示是首次申请   检查用户是否已申请店铺
            if ($param['id'] == 0) {
                $shop = model('certification')->get(['member_id' => $param['member_id']]);
                if ($shop) {
                    throw new \Exception('请勿重复申请！');
                }
            }

            // 组装店铺数据
            $data = [
                'proposer' => $param['proposer'],
                'card_no' => $param['card_no'],
                'status' => 1,
                'id_card_in_hand' => $param['id_card_in_hand'] . '-500',
                'id_card_front' => $param['id_card_front'] . '-500',
                'id_card_verso' => $param['id_card_verso'] . '-500'
            ];

            $model = model('certification');
            if ($param['id'] == 0) {
                $data['member_id'] = $param['member_id'];
                $model->save($data);
            } else {
                $model->save($data, ['id' => $param['id'], 'member_id' => $param['member_id']]);
            }

            Db::commit();
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }
}